package com.tiantian.novel.rest;

import com.tiantian.novel.model.NovelSummary;
import com.tiantian.novel.service.NovelService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * Novel controller
 *
 * @author wangkai
 * @since 2020/12/17.
 */
@RequestMapping("/novel")
@RestController
public class NovelController {
    private static final Logger log = LoggerFactory.getLogger(NovelController.class);

    @Autowired
    private NovelService novelService;

    /**
     * 查询小说信息
     *
     * @param url
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/query")
    public NovelSummary query(String url) throws Exception {
        log.info("query {}", url);
        return novelService.query(url);
    }

    /**
     * 抓取小说
     *
     * @param url
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/fetch")
    public NovelSummary fetch(String url) throws Exception {
        log.info("fetch {}", url);
        return novelService.fetch(url);
    }

    /**
     * 生成mobi
     *
     * @param url
     *
     * @return 任务id
     * @throws Exception
     */
    @GetMapping("/mobi/gen")
    public String genMobi(String url) throws Exception {
        log.info("genMobi 【{}】", url);
        return novelService.genMobi(url);
    }

    /**
     * 爬取 & 生成mobi
     *
     * @param url
     *
     * @return 任务id
     * @throws Exception
     */
    @GetMapping("/mobi/fetch_gen")
    public String fetchGenMobi(String url) throws Exception {
        log.info("fetchGenMobi {}", url);
        return novelService.fetchAndGenMobi(url);
    }

    /**
     * 下载mobi
     *
     * @param url
     *
     * @return 任务id
     * @throws Exception
     */
    @ApiOperation(value = "下载mobi", produces = "application/octet-stream")
    @GetMapping("/mobi/download")
    public String downloadMobi(String url, HttpServletResponse res) throws Exception {
        log.info("downloadMobi {}", url);
        File mobi = novelService.downloadMobi(url);
        String filename = mobi.getName();
        res.setCharacterEncoding("UTF-8");
        res.setContentType("application/octet-stream;charset=UTF-8");
        // 中文文件名处理
        res.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8"));
        byte[] buf = new byte[1024];
        try (FileInputStream fis = new FileInputStream(mobi); BufferedInputStream bis = new BufferedInputStream(fis)) {
            OutputStream out = res.getOutputStream();
            int len = 0;
            while ((len = bis.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
        } catch (Exception e) {
            log.error("", e);
        }
        return "SUCCESS.";
    }

    /**
     * 抓取小说
     *
     * @param code code
     *
     * @return NovelSummary
     * @throws Exception
     */
    @GetMapping("/fetchbycode")
    public NovelSummary fetchByCode(String code) throws Exception {
        return novelService.fetchByCode(code);
    }
}
